package org.examples.simplemulti;

import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
import org.apache.hc.core5.http.nio.DataStreamChannel;
import org.apache.hc.core5.http.nio.RequestChannel;
import org.apache.hc.core5.http.protocol.HttpContext;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Set;


public class BlockContentProducer implements AsyncRequestProducer {

    private HttpRequest request = null;

    private static String reqS1 = "--hsHFbol1iwlD51QOY-4SKHUnBE_fRRTK5xBgHkp\r\n" +
            "Content-Disposition: form-data; name=\"name1\"\r\n" +
            "Content-Type: application/json; charset=UTF-8\r\n\r\n";

    private static String reqS2 =
            "{\"k1\":\"v1\"}\r\n" +
                    "--hsHFbol1iwlD51QOY-4SKHUnBE_fRRTK5xBgHkp\r\n" +
                    "Content-Disposition: form-data; name=\"name2\"\r\n" +
                    "Content-Type: application/json; charset=UTF-8\r\n\r\n";

    private static String reqS3 =
            "{\"k2\":\"v2\"}\r\n" +
                    "--hsHFbol1iwlD51QOY-4SKHUnBE_fRRTK5xBgHkp--\r\n";

    private ByteBuffer buf1 = ByteBuffer.wrap(reqS1.getBytes(Charset.defaultCharset()));

    private ByteBuffer buf2 = ByteBuffer.wrap(reqS2.getBytes(Charset.defaultCharset()));

    private ByteBuffer buf3 = ByteBuffer.wrap(reqS3.getBytes(Charset.defaultCharset()));

    private AsyncEntityProducer dataProducer = new AsyncEntityProducer() {
        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public void failed(Exception e) {

        }

        @Override
        public long getContentLength() {
            return buf1.remaining() + buf2.remaining() + buf3.remaining();
        }

        @Override
        public String getContentType() {
            return "multipart/form-data; charset=ISO-8859-1; boundary=hsHFbol1iwlD51QOY-4SKHUnBE_fRRTK5xBgHkp";
        }

        @Override
        public String getContentEncoding() {
            return null;
        }

        @Override
        public boolean isChunked() {
            return false;
        }

        @Override
        public Set<String> getTrailerNames() {
            return null;
        }

        @Override
        public int available() {
            return 0;
        }

        @Override
        public void produce(DataStreamChannel dataStreamChannel) throws IOException {

        }

        @Override
        public void releaseResources() {

        }
    };

    public BlockContentProducer() {
//        this.request = new BasicHttpRequest("POST", new HttpHost("10.192.128.28",28166) , "/vocs/speak");
//        this.request = new BasicHttpRequest("POST", new HttpHost("47.108.196.76", 8080), "/post");
        this.request = new BasicHttpRequest("POST", new HttpHost("https","leiyang.icu", 443), "/post");
        this.dataProducer = dataProducer;
    }

    @Override
    public void sendRequest(final RequestChannel requestChannel, final HttpContext httpContext) throws HttpException, IOException {
        requestChannel.sendRequest(request, dataProducer, httpContext);
    }

    @Override
    public int available() {
        return dataProducer != null ? dataProducer.available() : 0;
    }

    @Override
    public void produce(final DataStreamChannel channel) throws IOException {
        channel.write(buf1);
//    channel.write(ByteBuffer.wrap("\r\n\r\n".getBytes(Charset.defaultCharset())));
        channel.write(buf2);
//    channel.write(ByteBuffer.wrap("\r\n\r\n".getBytes(Charset.defaultCharset())));
        channel.write(buf3);
//    channel.write(ByteBuffer.wrap("\r\n".getBytes(Charset.defaultCharset())));
        channel.endStream();
    }

    @Override
    public boolean isRepeatable() {
        return dataProducer == null || dataProducer.isRepeatable();
    }

    @Override
    public void failed(final Exception cause) {
        try {
            if (dataProducer != null) {
                dataProducer.failed(cause);
            }
        } finally {
            releaseResources();
        }
    }

    @Override
    public void releaseResources() {
        if (dataProducer != null) {
            dataProducer.releaseResources();
        }
    }
}
